﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SIS.ENT;
using SIS.BLL;
using SIS.DAL;

public partial class Master_DayGroupCodeSetUp : System.Web.UI.Page
{
         Int16 DayGroupCode = 0;
         
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.Params["id"] != null)
                {

                }
                else
                {
                    ClearDayGroupCode();
                    FillGrid();
                }
            }
            
        }

        private void FillGrid()
        {
            DayGroupCodeTableList lList = new DayGroupCodeTableList();
            lList = DayGroupCodeTableManager.GetList();
            grvDayGroupCode.DataSource = lList;
            grvDayGroupCode.DataBind();
            
        }       

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
                       string DayGroupCode = string.Empty;
                      if (chkMonday.Checked)
                        {
                            DayGroupCode = DayGroupCode +  chkMonday.Text + ";";
                        }
                         if (chkTuesday.Checked)
                         {
                              DayGroupCode = DayGroupCode + chkTuesday.Text + ";";
                         }
                         if (chkWednesday.Checked)
                         {
                             DayGroupCode = DayGroupCode + chkWednesday.Text + ";";
                         }
                         if (chkThursday.Checked)
                         {
                             DayGroupCode = DayGroupCode + chkThursday.Text + ";";
                         }
                        if (chkFriday.Checked)
                         {
                             DayGroupCode = DayGroupCode + chkFriday.Text + ";";
                         }
                           if (chkSaturday.Checked)
                         {
                             DayGroupCode = DayGroupCode + chkSaturday.Text + ";";
                         }
                          if (chkSunday.Checked)
                         {
                             DayGroupCode = DayGroupCode + chkSunday.Text + ";";
                         }
			 
                int  DayGroupCodeLength = DayGroupCode.Length ;
                string DayGroupCodeString = DayGroupCode.Substring(0, DayGroupCodeLength - 1);           

                DayGroupCodeTable objDayGroupCode = new DayGroupCodeTable();
                objDayGroupCode.DayGroupCode = Convert.ToInt16(lblDayGroupCode.Text);
                objDayGroupCode.DayDescriptionList = DayGroupCodeString;
                objDayGroupCode.Description = txtDescription.Text;
                objDayGroupCode.Status = true;
                objDayGroupCode.ModifiedDateTime = DateTime.Today;
                objDayGroupCode.CreatedDateTime = DateTime.Today;

                short lResult = DayGroupCodeTableManager.Save(objDayGroupCode);
                if (lResult > 0)
                {
                    ClearDayGroupCode();
                    lblMessage.Text = "Day Group Code Saved Successfully.";
                }
                else
                {
                    lblMessage.Text = "Day Group Code Saved Fail.";
                }
                btnSubmit.Text = "Save";
                FillGrid();
                

          }

        protected void FillValue()
        {
            ClearDayGroupCode();

            string DayDescriptionList = string.Empty;
            
            if(lblDayGroupCode.Text  != "-1")
            {
               DayGroupCode = Convert.ToInt16(lblDayGroupCode.Text);
            }
            DayGroupCodeTable objDayGroupCode = new DayGroupCodeTable();
            objDayGroupCode = DayGroupCodeTableManager.GetItem(DayGroupCode);
            if (objDayGroupCode != null)
            {
                lblDayGroupCode.Text = Convert.ToString(objDayGroupCode.DayGroupCode);
                DayDescriptionList = objDayGroupCode.DayDescriptionList;          
                txtDescription.Text = objDayGroupCode.Description;
            }
            string[] arrayDayGroupCode = DayDescriptionList.Split(';');
            foreach (string item in arrayDayGroupCode)
            {
                //foreach (ListItem li in chkDayGroupList.Items)
                //{
                    switch (item)
                    {
                        case   "Monday" :
                             chkMonday.Checked = true;
                            break ;                       
                        case "Tuesday":
                            chkTuesday.Checked = true;
                            break;
                        case "Wednesday":
                            chkWednesday.Checked = true;
                            break;
                        case "Thursday":
                            chkThursday.Checked = true;
                            break;
                        case "Friday":
                            chkFriday.Checked = true;
                            break;
                        case "Saturday":
                            chkSaturday.Checked = true;
                            break;
                        default:
                            chkSunday.Checked = true;
                            break;
                   // }
                    
                }
            }
              
        }

        protected void grvDayGroupCode_SelectedIndexChanged(object sender, EventArgs e)
        {
            ClearDayGroupCode();
            btnSubmit.Text = "Update";

            if (grvDayGroupCode.SelectedIndex >= 0)
            {
                DataKey _selectedDataKey = grvDayGroupCode.SelectedDataKey;
                Int16 _DayGroupCodeKey = Convert.ToInt16(_selectedDataKey[0]);
                
                if (_DayGroupCodeKey > 0)
                {
                    GridViewRow _selectedRow = grvDayGroupCode.SelectedRow;

                    LinkButton lnkEdit = (LinkButton)_selectedRow.FindControl("lnkbtnEdit");
                    //lnkEdit.Enabled = false;
                    lblDayGroupCode.Text = Convert.ToString(_DayGroupCodeKey);
                    FillValue();                   

                }
            }
        }

        private void ClearDayGroupCode()
        {
            chkMonday.Checked = false;
            chkTuesday.Checked = false;
            chkWednesday.Checked = false;
            chkThursday.Checked = false;
            chkFriday.Checked = false;
            chkSaturday.Checked = false;
            chkSunday.Checked = false;
            txtDescription.Text = string.Empty;
            
        }

        protected void grvDayGroupCode_OnRowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer;
                Int16 _DayGroupCodeKey = Convert.ToInt16(grvDayGroupCode.DataKeys[row.RowIndex].Value);
                bool lResult = DayGroupCodeTableManager.Delete(_DayGroupCodeKey);
                if (lResult)
                {
                    lblMessage.Text = "Deleted Successfully";
                }
                else
                {
                    lblMessage.Text = "Delet not Success";
                }
            }
            FillGrid();

        }

        protected void grvDayGroupCode_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

            Int16 _grvDayGroupCodeKey = Convert.ToInt16(grvDayGroupCode.DataKeys[e.RowIndex].Value);
            bool lResult = DayGroupCodeTableDB.Delete(_grvDayGroupCodeKey);
            if (lResult)
            {
                lblMessage.Text = "Deleted Successfully";
            }
            else
            {
                lblMessage.Text = "Delet not Success";
            }
            FillGrid();
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Master/DayGroupCodeSetUp.aspx");
        }
}

 